#=
测试作用量的计算
=#


#include("../src/pimary.jl")
push!(LOAD_PATH, "/Users/mary/workspace/")
using pimary

using Plots


"""
绘制坐标
"""
function draw_sket!(plt, Nt, vol)
    for it in Base.OneTo(Nt)
        plot!(plt, [0.0, vol[1]], [it, it], line=:dot, label="", color=:black)
    end
    plot!(plt, [0.0, vol[1]], [Nt+1, Nt+1], line=:dot,
    xlims=(0.0, vol[1]), ylims=(0.0, Nt+2), label="", color=:black)
end


"""
绘制beads
"""
function draw_beads!(plt, ti, rpos, clr)
    for i in Base.OneTo(length(rpos))
        annotate!(plt, rpos[i], ti, ("$(i)", 10, :black, :center))
        #plot!(plt, Plots.partialcircle(0, 2π, 100))
    end
    scatter!(plt, rpos, repeat([ti], length(rpos)), markersize=15, label="", color=clr)
end


"""
绘制连线
"""
function draw_conns!(plt, ti, rpos, rprev, clr)
    for i in Base.OneTo(length(rpos))
        plot!(plt, [rpos[i], rprev[i]], [ti, ti-1], label="", color=clr)
    end
end




function run()
    Nt = 5
    Np = 2
    Ne = 1
    vol = [2.0, 2.0, 2.0]
    fbup = WrdFb(Float64, Np, Nt)
    fbdn = WrdFb(Float64, Np, Nt)
    fbne = WrdFb(Float64, Ne, Nt)
    uppos = rand(Np, 3)
    dnpos = rand(Np, 3)
    nepos = rand(Ne, 3)
    for id in Base.OneTo(3)
        uppos[:, id] = uppos[:, id]*vol[id]
        dnpos[:, id] = dnpos[:, id]*vol[id]
        nepos[:, id] = nepos[:, id]*vol[id]
    end
    offup = (rand(Nt-1, Np, 3).-0.5)*pimary._ThermWL
    offdn = (rand(Nt-1, Np, 3).-0.5)*pimary._ThermWL
    for ip in Base.OneTo(Np)
        fbup[ip][1, :] = uppos[ip, :]
        fbdn[ip][1, :] = dnpos[ip, :]
        for it in 2:1:Nt
            fbup[ip][it, :] = uppos[ip, :] + offup[it-1, ip, :]
            fbdn[ip][it, :] = dnpos[ip, :] + offdn[it-1, ip, :]
        end
    end
    for ie in Base.OneTo(Ne); for it in Base.OneTo(Nt)
        fbne[ie][it, :] = nepos[ie, :]
    end; end
    #
    rhov, rhop, rhok = spinful_rho_path(fbup, fbdn, fbne, vol)
    println(rhov)
    println(rhop)
    println(rhok)
    #
    # 绘制世界线
    #
    plt = plot()
    #up
    draw_sket!(plt, Nt, vol)
    for ti in Base.OneTo(Nt)
        xpos = beads(fbup, ti)[:, 1]
        draw_beads!(plt, ti, xpos, :red)
        if ti > 1
            draw_conns!(plt, ti, xpos, beads(fbup, ti-1)[:, 1], :red)
        end
    end
    xprev = beads(fbup, Nt)[:, 1]
    xpos = beads(fbup, 1)[:, 1]
    draw_beads!(plt, Nt+1, xpos, :red)
    draw_conns!(plt, Nt+1, xpos, xprev, :red)
    #dn
    for ti in Base.OneTo(Nt)
        xpos = beads(fbdn, ti)[:, 1]
        draw_beads!(plt, ti, xpos, :green)
        if ti > 1
            draw_conns!(plt, ti, xpos, beads(fbdn, ti-1)[:, 1], :green)
        end
    end
    xprev = beads(fbdn, Nt)[:, 1]
    xpos = beads(fbdn, 1)[:, 1]
    draw_beads!(plt, Nt+1, xpos, :green)
    draw_conns!(plt, Nt+1, xpos, xprev, :green)
    #core
    for ti in Base.OneTo(Nt)
        xpos = beads(fbne, ti)[:, 1]
        draw_beads!(plt, ti, xpos, :blue)
        if ti > 1
            draw_conns!(plt, ti, xpos, beads(fbne, ti-1)[:, 1], :blue)
        end
    end
    xprev = beads(fbne, Nt)[:, 1]
    xpos = beads(fbne, 1)[:, 1]
    draw_beads!(plt, Nt+1, xpos, :blue)
    draw_conns!(plt, Nt+1, xpos, xprev, :blue)
    savefig("test.png")
end

run()

